﻿using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;

namespace TestGame {
    /// <summary>
    /// The Screen in charge of gathering the information needed to connect
    /// to another computer.
    /// </summary>
    class ConnectionScreen : MenuScreen {
        #region Fields

        InputEntry ipEntry;
        InputEntry portEntry;
        MenuEntry acceptEntry;
        MenuEntry backEntry;

        const int MaxPortSize = 5;
        const int MaxIPAdressSize = 15;

        IPAddress address;
        int port;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructs a ConnectionScreen
        /// </summary>
        public ConnectionScreen() : base("Connect to Player") {
            ipEntry = new InputEntry("IP Address: ", AcceptableInput.Numbers);
            portEntry = new InputEntry("Port: ", AcceptableInput.Numbers);
            acceptEntry = new MenuEntry("Accept");
            backEntry = new MenuEntry("Back");

            ipEntry.PeriodEnabled = true;
            ipEntry.MaxLength = MaxIPAdressSize;

            portEntry.Data = MyGame.Port.ToString();
            portEntry.MaxLength = MaxPortSize;
            portEntry.UpdateText();

            acceptEntry.Selected += OnAcceptSelected;
            backEntry.Selected += OnCancel;

            MenuEntries.Add(ipEntry);
            MenuEntries.Add(portEntry);
            MenuEntries.Add(acceptEntry);
            MenuEntries.Add(backEntry);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called upon the Accept menu entry being selected. It checks the 
        /// user input for some form of validity, then asks the user to confirm
        /// any changes the double-checking may have done.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnAcceptSelected(object sender, PlayerIndexEventArgs e) {
            try {
                if (IPAddress.TryParse(ipEntry.Data, out address)) {
                    if (int.TryParse(portEntry.Data, out port)) {
                        const String message = "IP Address: [{0}]\nPort: [{1}]\nIs this information correct?";
                        String text = String.Format(message, address.ToString(), port.ToString());
                        ConfirmMessageBoxScreen screen = new ConfirmMessageBoxScreen(text);

                        screen.Accepted += OnConfirm;

                        ScreenManager.AddScreen(screen, ControllingPlayer);
                    }
                    else {
                        MessageBoxScreen screen = new MessageBoxScreen("Bad port!", false);

                        ScreenManager.AddScreen(screen, ControllingPlayer);
                    }
                }
                else {
                    MessageBoxScreen screen = new MessageBoxScreen("Bad IP Address!", false);

                    ScreenManager.AddScreen(screen, ControllingPlayer);
                }
            }
            catch (Exception exception) {
                Console.WriteLine("Parsing Error!" + exception.ToString());
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called when the ConfirmMessageBox is accepted and the information
        /// is finalized.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnConfirm(object sender, PlayerIndexEventArgs e) {
            IPEndPoint ipInfo = new IPEndPoint(address, port);
            PeerMultiplayerScreen screen = new PeerMultiplayerScreen(ipInfo, false);

            ScreenManager.AddScreen(screen, ControllingPlayer);
        }

        #endregion

    }
}
